﻿using System;
using BEGDC.CC.BL.Common;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.UserManagement;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.UserControls
{
	/// <summary>
	/// Summary description for BaseCtrl.
	/// </summary>
	public class BaseCtrl : System.Web.UI.UserControl
	{
		public BaseCtrl()
		{
		}

		protected virtual void ButtonStatusSetting()
		{
		}

		#region Properties: AppID, Application
		//updated by Joe 2007/5/23
		//add keyword virtual
		/// <summary>
		/// 
		/// </summary>
		protected virtual int AppID
		{
			get
			{ 
				if( this.ViewState["AppID"] == null )
				{
					if( Request["AppID"] == null ) return 0;
					else return Convert.ToInt32( Request["AppID"] );
				}
				return (int)this.ViewState["AppID"]; 
			}
			set{ this.ViewState["AppID"] = value; }
		}

		protected new virtual BEGDC.CC.Entity.Application Application
		{
			get
			{	if( Session[this.AppID.ToString()] != null ) 
					return (BEGDC.CC.Entity.Application)Session[this.AppID.ToString()];
				return null;
			}
			set{ Session[this.AppID.ToString()] = value; }
		}

		#endregion


		#region Check user's accessibility
		
		protected virtual Accessibility CheckAccessibility(string nameEN, string roleName)
		{
			Accessibility access = Accessibility.None;
			try
			{
				IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
				if( userService.IsInRole( nameEN, roleName ) )
				{
					access = Accessibility.Full;
				}
				else
				{
					access = Accessibility.Readonly;
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}

			return access;
		}

		protected virtual Accessibility CheckAccessibility(string stepName, string roleName, string nameEN, int appId)
		{
			Accessibility access = Accessibility.None;
			try
			{
				IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
				if( userService.IsInRole(stepName, roleName, nameEN, appId) )
				{
					access = Accessibility.Full;
				}
				else
				{
					access = Accessibility.Readonly;
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}

			return access;
		}

		/// <summary>
		/// disable all button and imagebutton in the ctrl 
		/// </summary>
		/// <param name="ctrl"></param>
		protected virtual void DisableChildrenControls(System.Web.UI.Control parentControl)
		{
			foreach(System.Web.UI.Control child in parentControl.Controls)
			{
				DisableChildrenControls(child);
				Type type = child.GetType();
				if (IsClassOf(type, typeof(System.Web.UI.WebControls.TextBox))
					|| IsClassOf(type, typeof(System.Web.UI.WebControls.ListBox))
					|| IsClassOf(type, typeof(System.Web.UI.WebControls.DropDownList))
					|| IsClassOf(type, typeof(System.Web.UI.WebControls.CheckBoxList))
					|| IsClassOf(type, typeof(System.Web.UI.WebControls.CheckBox))
					|| IsClassOf(type, typeof(System.Web.UI.WebControls.RadioButtonList))
					|| IsClassOf(type, typeof(System.Web.UI.WebControls.RadioButton)))
				{
					System.Web.UI.WebControls.WebControl control = (System.Web.UI.WebControls.WebControl)child;
					control.Enabled = false;
					continue;
				}
				if (IsClassOf(type, typeof(System.Web.UI.WebControls.ImageButton))
					|| IsClassOf(type, typeof(System.Web.UI.WebControls.Button)))
				{
					child.Visible = false;
					continue;
				}
				if (IsClassOf(type, typeof(System.Web.UI.HtmlControls.HtmlImage)))
				{
					System.Web.UI.HtmlControls.HtmlControl control = (System.Web.UI.HtmlControls.HtmlControl)child;
					control.Disabled = true;
					continue;
				}
			}
		}
	
		public static bool IsClassOf(Type objType, Type type)
		{
			if (objType.Equals(type) || objType.IsSubclassOf(type))
				return true;
			return false;
		}

		protected AccessControl Access
		{
			get
			{
				try
				{
					IPageAccess page = (IPageAccess)this.Page;
					if( page != null )
					{
						return page.Access;
					}
					else
						return null;
				}
				catch
				{
					return null;
				}
			}
		}

		#endregion
	}
}
